/*
   @Copyright:LeetCode
   @Author:   tjyemail
   @Problem:  http://leetcode.com/problems/remove-duplicates-from-sorted-array-ii
   @Language: C++
   @Datetime: 19-07-08 11:35
   */

// Method 1
class Solution {
public:
	int removeDuplicates(vector<int>& nums) {
		int len=0;
		for(int i=0, j=0; i<nums.size(); i=j){
			for(j=i+1; j<nums.size() && nums[i]==nums[j]; ++j);
			for(int k=0; k<2 && i+k<j; nums[len++]=nums[i+k++]);
		}
		return len;
	}
};

// Method 2
class Solution {
public:
	int removeDuplicates(vector<int>& nums) {
		int len=0;
		for(int i=0; i<nums.size(); ++i)
			if(len<2||nums[i]!=nums[len-2]) nums[len++]=nums[i];
		return len;
	}
};

